package org.dreamfly.dreamalarm.widget;

import org.dreamfly.dreamalarm.R;
import org.dreamfly.dreamalarm.bean.Alarm;

import android.content.Context;
import android.util.AttributeSet;

public class RepeatMutiSelectListPreference extends MutiSelectListPreference {

	private final String workdayValue="abcde";
	private final String weekendVlaue="fg";
	private final String everydayVlaue="abcdefg";
	
	public RepeatMutiSelectListPreference(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	public RepeatMutiSelectListPreference(Context context, AttributeSet attrs){
		super(context,attrs);
	}
	
	@Override
	public void setValue (String value){
		super.setValue(value);
		refreshSummary();
	}
	
	@Override
	public String getValue(){
		String value=super.getValue();
		int result=0;
		for(int i=0;i<value.length();i++){
			switch(value.charAt(i)){
			case 'a':
				result+=1000000;
				break;
			case 'b':
				result+=100000;
				break;
			case 'c':
				result+=10000;
				break;
			case 'd':
				result+=1000;
				break;
			case 'e':
				result+=100;
				break;
			case 'f':
				result+=10;
				break;
			case 'g':
				result+=1;
				break;
			}
		}
		return ""+result;
	}
	
    private void refreshSummary (){
    	if(getValue().equals(workdayValue)){
    		setSummary(R.string.repeat_workdays);
    	}else if(getValue().equals(weekendVlaue)){
    		setSummary(R.string.repeat_weekend);
    	}else if(getValue().equals(everydayVlaue)){
    		setSummary(R.string.repeat_everyday);
    	}
    }

    public void initChoose(int repeat){
    	int step=1000000;    //用于取出每日的是否重复
		StringBuilder sb=new StringBuilder();
		for(int i=0;i<7;i++){
			if(repeat/step==Alarm.REPEAT_TRUE){
				sb.append(this.getEntryValues()[i]);
				mClickedDialogEntryIndices[i]=true;
			}
			step=step/10;
		}
		this.setValue(sb.toString());
    }
}
